package com.yuyang.gulimall.search.controller;

import com.yuyang.common.exception.BizCodeEnum;
import com.yuyang.common.to.es.SkuEsModel;
import com.yuyang.common.utils.R;
import com.yuyang.gulimall.search.service.ElasticSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author yu yang
 * @date 2023/1/7 -22:50
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {
    @Autowired
    ElasticSaveService elasticSaveService;
    //上架商品
    @PostMapping("/product")
    public R productStartUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean b = true;
        try {
            b = elasticSaveService.productStartUp(skuEsModels);
        } catch (IOException e) {
            log.info("商品上架出错");
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if (!b){
            return R.ok();
        }else{
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }
}
